﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace Acoris.Core
{
    using Acoris.Document;
    using Acoris.Layout;
    using Acoris.Rendering;

    using Microsoft.Graphics.Canvas;
    using Microsoft.Graphics.Canvas.Brushes;
    using Microsoft.Graphics.Canvas.UI;
    using Microsoft.Graphics.Canvas.UI.Xaml;    
    using Windows.UI.Text;
    using Windows.UI.Text.Core;

    enum MoveOperation
    {
        Up,Down,Left,Right
    }

    interface ICoreTextService
    {

        void Enter();
        void Exit();

        /// <summary>
        /// 用户请求一个替换操作
        /// </summary>
        event Action<int,int,string> ReplaceOperation;

        /// <summary>
        /// 用户请求一个选择文本操作
        /// </summary>
        event Action<int,int> SelectOperation;

        /// <summary>
        /// 用户请求一个插入操作
        /// </summary>
        event Action<int,string> InsertOperation;

        /// <summary>
        /// 用户请求一个删除操作
        /// </summary>
        event Action<int ,int> DeleteOperation;

        /// <summary>
        /// 用户请求一个移动指针操作
        /// </summary>
        event Action<MoveOperation> MovePointerOperation;
    }

    public sealed class AcorisTextEditor : ContentControl
    {
        #region Internal Class & 用于维护文本输入的内部类

        /// <summary>
        /// 用于维护文本输入状态的服务类
        /// </summary>
        private class CoreTextService:ICoreTextService
        {
            CoreTextServicesManager _manager;
            CoreTextEditContext _context;
            bool _focusState;

            public CoreTextService()
            {
                _manager = CoreTextServicesManager.GetForCurrentView();
                _context = _manager.CreateEditContext();
                _context.CompositionCompleted += OnCompositionCompleted;
                _context.CompositionStarted += OnCompositionStarted;
                _context.FocusRemoved += OnFocusRemoved;
                _context.FormatUpdating += OnFormatUpdating;
                _context.LayoutRequested += OnLayoutRequested;
                _context.NotifyFocusLeaveCompleted += OnNotifyFocusLeaveCompleted;
                _context.SelectionRequested += OnSelectionRequested;
                _context.SelectionUpdating += OnSelectionUpdating;
                _context.TextRequested += OnTextRequested;
                _context.TextUpdating += OnTextUpdating;
                _focusState = false;
            }

            #region Internal Event Handler & 内部事件处理器

            private void OnTextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs args)
            {
                throw new NotImplementedException();
            }

            private void OnTextRequested(CoreTextEditContext sender, CoreTextTextRequestedEventArgs args)
            {
                throw new NotImplementedException();
            }

            private void OnSelectionUpdating(CoreTextEditContext sender, CoreTextSelectionUpdatingEventArgs args)
            {
                throw new NotImplementedException();
            }

            private void OnSelectionRequested(CoreTextEditContext sender, CoreTextSelectionRequestedEventArgs args)
            {
                throw new NotImplementedException();
            }

            private void OnNotifyFocusLeaveCompleted(CoreTextEditContext sender, object args)
            {
                throw new NotImplementedException();
            }

            private void OnLayoutRequested(CoreTextEditContext sender, CoreTextLayoutRequestedEventArgs args)
            {
                throw new NotImplementedException();
            }

            private void OnFormatUpdating(CoreTextEditContext sender, CoreTextFormatUpdatingEventArgs args)
            {
                throw new NotImplementedException();
            }

            private void OnFocusRemoved(CoreTextEditContext sender, object args)
            {
                throw new NotImplementedException();
            }

            private void OnCompositionStarted(CoreTextEditContext sender, CoreTextCompositionStartedEventArgs args)
            {
                throw new NotImplementedException();
            }

            private void OnCompositionCompleted(CoreTextEditContext sender, CoreTextCompositionCompletedEventArgs args)
            {
                throw new NotImplementedException();
            }

            #endregion

            #region Events & 事件

            public event Action<int, int, string> ReplaceOperation;
            public event Action<int, int> SelectOperation;
            public event Action<int, string> InsertOperation;
            public event Action<int, int> DeleteOperation;
            public event Action<MoveOperation> MovePointerOperation;

            #endregion

            public void Enter()
            {
                throw new NotImplementedException();
            }

            public void Exit()
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region Fields & 字段

        private ICoreTextService _textService;

        #endregion

        #region Constructor & 构造函数

        public AcorisTextEditor()
        {
            this.DefaultStyleKey = typeof(AcorisTextEditor);
            this._textService = new CoreTextService();
            var canvas = new CanvasControl();
            canvas.Draw += OnCanvasDraw;
            canvas.CreateResources += OnCreateResources;
            this.Content = canvas;
        }

        #endregion

        #region Method & 方法


        #endregion

        #region Property & 属性


        /// <summary>
        /// 获取当前文本的字符串值
        /// </summary>
        public string Text {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        /// <summary>
        /// Text属性的依赖属性
        /// </summary>
        public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(AcorisTextEditor), new PropertyMetadata(null));



        #endregion

        #region Event & 事件

        #endregion

        private void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            
        }

        private void OnCanvasDraw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            
        }
    }
}
